class QaController < ApplicationController
  before_filter :login_required
  def new
    @category_list = Category.find(:all)
  end

  # TODO: トランザクションの考慮がまるっきり抜けてる
  def create
    # Question
    q = Question.new
    q.question_type = params[:question_type]
    q.question_text = params[:question_ta].gsub("\n", "<br />")
    q.is_public = 1 # TODO: 設定から取れるようにする
    q.user_id = current_user
    q.category_id = params[:category]
    q.save

    # Selection
    if params[:question_type] != "3"
      selections = params[:selection]
      is_collect_list = params[:is_collect]
      
      selections.each_pair do |key, value|
        next if value.strip.empty? 

        s = Selection.new
        s.selection_text = value
        s.is_collect = is_collect_list[key] ? 1 : 0
        s.question_id = q.id
        s.user_id = current_user
        s.save
      end
    end

    # Answer
    if params[:question_type] == "3"
      a = Answer.new
      a.answer_text = params[:answer_ta]
      a.question_id = q.id
      a.user_id = current_user
      a.save
    end

    # Description
    d = Description.new
    d.description_text = params[:description_ta].gsub("\n", "<br />")
    d.question_id = q.id
    d.user_id = current_user
    d.save

    redirect_to :action => :new
  end
  
  def quiz
    @question = Question.find(params[:question_id].to_i)
    @selections = Selection.find(:all, :conditions => "question_id = #{@question.id}")
    @answer = Answer.find(:all, :conditions => "question_id = #{@question.id}")
    @description = Description.find(:all, :conditions => "question_id = #{@question.id}")
  end

  def answer
    question_id = params[:question_id]
    question_type = params[:question_type]
    answer = params[:answer]

    # TODO: ENUMにする / case whenにする
    if question_type == "1"
      result = Selection.count(:conditions => ["id = ? and question_id = ? and is_collect = '1'",
                                              answer, question_id])
      @result_text = ""
      @is_collect = ""
      if result > 0
        @description = Description.find(:first, :conditions => ["question_id = ?", question_id])
        @result_text = "○ 正解です！"
        @is_collect = "1"
      else
        @result_text = "× 残念です！"
        @is_collect = "0"
      end
    elsif question_type == "2"
      user_answers = params[:answer].values
      selections = Selection.find(:all, :conditions => ["question_id = ?", question_id])
      answers = selections.map {|s| s.id.to_s if s.is_collect == 1}.compact
      if (answers & user_answers).size == answers.size
        @description = Description.find(:first, :conditions => ["question_id = ?", question_id])
        @result_text = "○ 正解です！"
        @is_collect = "1"
      else
        @result_text = "× 残念です！"
        @is_collect = "0"
      end
    elsif question_type == "3"
      answer = params[:answer]
      p answer
      result = Answer.count(:conditions => ["question_id = ? and answer_text = ?",
                                            question_id, answer])
      if result > 0
        @description = Description.find(:first, :conditions => ["question_id = ?", question_id])
        @result_text = "○ 正解です！"
        @is_collect = "1"
      else
        @result_text = "× 残念です！"
        @is_collect = "0"
      end
    else
      # must not happend
    end
  end

  def edit
    question_id = params[:id]
    @question = Question.find(question_id)
    @selections = Selection.find(:all, :conditions => "question_id = #{@question.id}")
    @answer = Answer.find(:first, :conditions => "question_id = #{@question.id}")

    @description = Description.find(:first, :conditions => "question_id = #{@question.id}")
  end
  
  def update
    # TODO: とりあえず動かしたいので適当に書いた。ダッサイので、直す。
    # formにquizのような連想配列をつくって、update_attributesで一気に更新したい
    @question = Question.find(params[:question_id])
    @question.update_attribute("question_text", params[:question_ta])
    @question.update_attribute("question_type", params[:question_type])
    @question.update_attribute("category_id", params[:category])

    @selections = Selection.find(:all, :conditions => ["question_id = ?", params[:question_id]])
    @selections.each do |selection|
      selection.update_attribute("selection_text", params["selection"][selection.id.to_s])
      selection.update_attribute("is_collect", params["is_collect"][selection.id.to_s] == "on" ? 1 : 0)
    end

    if params[:question_type] == "3"
      @answer = Answer.find(:first, :conditions => ["question_id = ?", params[:question_id]])
      @answer.update_attribute("answer_text", params[:answer_ta])
    else
      # TODO: レコード数を調べて、0件だったら追加する処理
    end

    @description = Description.find(:first, :conditions => ["question_id = ?", params[:question_id]])
    @description.update_attribute("description_text", params[:description_ta])

    redirect_to :controller => 'mypage'
  end
  
  # TODO: deleteとdestoryはどっちがいいかを検討する
  def destroy
    @question = Question.find(params[:id])
    @question.destroy
    redirect_to :controller => 'mypage'
  end
end
